﻿using System;

namespace SoftwareConsulting.BI11.NumericalMethods.Integration
{   
    /// <summary>
    /// Класс метода Симпсона
    /// </summary>
    public class SimpsonIntegration 
    {
        private const double startH = 0.015f;
        private double[,] _result;

        /// <summary>
        /// Конструктор по умолчанию
        /// </summary>
        /// <param name="function">Функция, требующая решения</param>
        /// <param name="a">Нижняя граница интеграла</param>
        /// <param name="b">Верхняя граница интеграла</param>
        /// <param name="pointsNumber">Число точек</param>
        public SimpsonIntegration(FunctionWithOneArgument function, double a, double b, int pointsNumber)
        {
            double fi = 0;
            double I = 0;
            double h = startH;
            int j = 0;            
            _result = new double[2, pointsNumber+1];
            for (int i = 0; i < pointsNumber; i++)
            {
                fi = a + h;
                do
                {
                    if (j % 2 == 0)
                    {
                        I = I + 2 * function(fi);
                    }
                    else
                    {
                        I = I + 4 * function(fi);
                    }
                    fi = fi + h;
                    j = j + 1;
                } 
                while (fi < b);
                I = I + function(a) + function(b);
                I = I * (h / 3);
                _result[0, i] = I;
                _result[1, i] = h;
                I = 0;
                h += 0.001f;
            }
        }

        /// <summary>
        /// Возвращает решение
        /// </summary>        
        public double[,] GetSolution()
        {
            return _result;
        }
    }
}
